package com.yw.smartcity_study;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;
import com.youth.banner.listener.OnPageChangeListener;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

public class SplashActivity extends AppCompatActivity {

    private Banner banner;
    private Button setNetBt;
    private Button gotoAppBt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //判断是否曾经进入过APP， 进入过就不在展示  直接进入login页面
        boolean isSHowGuide = SPUtil.getInstance().getBoolean("is_show_guide", false);
        if(isSHowGuide){
            startActivity(new Intent(SplashActivity.this, LoginActivity.class));
            finish();
            return;
        }

        setContentView(R.layout.activity_splash);

        banner = (Banner) findViewById(R.id.banner);


        setNetBt = (Button) findViewById(R.id.setNetBt);
        gotoAppBt = (Button) findViewById(R.id.gotoAppBt);


        List<Integer> images = new ArrayList<>();
        images.add(R.drawable.smartcity1);
        images.add(R.drawable.smartcity2);
        images.add(R.drawable.smartcity3);
        images.add(R.drawable.smartcity4);
        images.add(R.drawable.smartcity5);

        banner.setAdapter(new BannerImageAdapter<Integer>(images) {

            @Override
            public void onBindView(BannerImageHolder bannerImageHolder, Integer integer, int i, int i1) {
                bannerImageHolder.imageView.setImageResource(integer);
            }
        });
        banner.isAutoLoop(false);//不自动轮播
        banner.setIndicator(new CircleIndicator(this));//设置小圆点
        banner.start();

        banner.addOnPageChangeListener(new OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                if(i == 4){
                    setNetBt.setVisibility(View.VISIBLE);
                    gotoAppBt.setVisibility(View.VISIBLE);
                }else{
                    setNetBt.setVisibility(View.GONE);
                    gotoAppBt.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

        setNetBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //弹窗一个对话框
                EditText editText = new EditText(v.getContext());
                editText.setHint("输入格式：192.168.0.1:10001");
                //editText.set
                new AlertDialog.Builder(v.getContext())
                        .setView(editText)
                        .setTitle("端口IP设置")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                //保存editText的值
                                //editText.getText();

                                dialog.dismiss();
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        })
                        .create()
                        .show();
            }
        });

        gotoAppBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(SplashActivity.this, LoginActivity.class));
                //保存一个标记位  sharepreference
                SPUtil.getInstance().saveBoolean("is_show_guide", true);


                finish();
            }
        });


    }
}